﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class Controls_admin_ModuleByCates : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                base.OpenConnection();
                loadCategories();
                loadModule();
            }
            catch { }
            finally { base.CloseConnection(); }
        }
    }
    private DataTable loadSource(int c_id, DataTable objDT, int loca, string name)
    {
        DataTable dtTop;
        if (c_id != -1 && c_id != -2)
            dtTop = BO_Module.SelectLocationAdmin(loca, c_id);
        else if (c_id == -1)
            dtTop = BO_Module.SelectLocationHomeAdmin(loca);
        else
            dtTop = BO_Module.SelectLocation(loca);
        if (dtTop != null && dtTop.Rows.Count > 0)
        {
            string tip = String.Empty;
            string m_id = String.Empty;
            foreach (DataRow drow in dtTop.Rows)
            {
                m_id += drow["MODULE_ID"].ToString() + ";";
                tip += drow["FULL_NAME"].ToString() + ",";
            }
            tip = tip.Substring(0, tip.Length - 1);
            DataRow dr = objDT.NewRow();
            dr["LOCATION_ID"] = loca;
            dr["LOCATION"] = name;
            dr["COUNT"] = dtTop.Rows.Count;
            dr["TIP"] = tip;
            dr["MODULE_ID"] = m_id;
            objDT.Rows.Add(dr);
        }
        return objDT;
    }
    protected void loadModule()
    {
        try
        {
            int c_id = Convert.ToInt32(ddlCategories.SelectedValue);
            if (c_id != -1 && c_id != -2)
            {
                DataTable categoryDT = BO_Categories.Search(c_id);
                if (categoryDT != null && categoryDT.Rows.Count > 0)
                    title.InnerText = "Vị trí các Modules thuộc danh mục " + categoryDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
            }
            else if (c_id == -1)
                title.InnerText = "Tất cả các Modules theo vị trí";
            else
                title.InnerText = "Modules ngoài trang chủ theo vị trí";
            DataTable objDT = new DataTable();
            objDT.Columns.Add("LOCATION_ID");
            objDT.Columns.Add("LOCATION");
            objDT.Columns.Add("COUNT");
            objDT.Columns.Add("TIP");
            objDT.Columns.Add("MODULE_ID");
            objDT = loadSource(c_id, objDT, 1, "Banner Modules");
            objDT = loadSource(c_id, objDT, 2, "Modules giữa trang");
            objDT = loadSource(c_id, objDT, 3, "Modules bên phải");
            if (objDT != null && objDT.Rows.Count > 0)
            {
                rptModule.DataSource = objDT;
                rptModule.DataBind();
            }
            else
            {
                rptModule.DataSource = null;
                rptModule.DataBind();
                divMessage.InnerHtml = O2S_Message.Warning("Thông Báo", "Hiện tại chưa có Module nào .");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnLOCATION_ID = (Button)e.Item.FindControl("btnLOCATION_ID");
            int location_id = Convert.ToInt32(btnLOCATION_ID.CommandArgument);
            string module_idList = e.CommandArgument.ToString();
            string[] module_idArr = module_idList.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string module_id in module_idArr)
            {
                BO_Module.Delete(Convert.ToInt32(module_id));
            }
            loadModule();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadModule();
    }
    private void loadCategories()
    {
        ddlCategories.Items.Clear();
        ddlCategories.Items.Add(new ListItem("Modules trang chủ", "-2"));
        ddlCategories.Items.Add(new ListItem("Tất cả các Modules", "-1"));
        ddlCategories.Items.Add(new ListItem("Đăng ký", "-3"));
        ddlCategories.Items.Add(new ListItem("Quên mật khẩu", "-4"));
        ddlCategories.Items.Add(new ListItem("Shows Chi tiết", "-5"));
        ddlCategories.Items.Add(new ListItem("Tin chi tiết", "-6"));
        ddlCategories.Items.Add(new ListItem("Liên hệ", "-7"));
        base.loadCategories(ddlCategories, 0);
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        if (ddlCategories.SelectedValue.Equals("-1") || ddlCategories.SelectedValue.Equals("-2"))
            Response.Redirect("default.aspx?module=moduleadd");
    }
}